package sddz.foodmanage.activity;

import android.Manifest;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanzhenjie.permission.PermissionListener;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import sddz.foodmanage.R;
import sddz.foodmanage.base.BaseActivity;
import sddz.foodmanage.utils.CallWindow;
import sddz.foodmanage.utils.DataCleanManager;
import sddz.foodmanage.utils.SnackbarHelper;
import sddz.foodmanage.utils.UpdateManager;

/**
 * @Date:2019/7/3
 * @Author:lbb
 * 关于我们
 */

public class AboutUsActivity extends BaseActivity {

    @BindView(R.id.title_navigation_icon)
    ImageView titleNavigationIcon;
    @BindView(R.id.title_text)
    TextView titleText;
    @BindView(R.id.title_menu_icon)
    ImageView titleMenuIcon;
    @BindView(R.id.about_us)
    TextView aboutUs;
    @BindView(R.id.about_us_version)
    TextView aboutUsVersion;
    @BindView(R.id.about_tv_weixin)
    TextView aboutTvWeixin;
    @BindView(R.id.about_phone)
    TextView aboutPhone;
    @BindView(R.id.about_url)
    TextView aboutUrl;
    @BindView(R.id.about_service)
    TextView aboutService;
    @BindView(R.id.textView2)
    TextView textView2;
    @BindView(R.id.more_update)
    TextView moreUpdate;
    @BindView(R.id.relative_check_update)
    AutoRelativeLayout relativeCheckUpdate;
    @BindView(R.id.more_helper)
    TextView moreHelper;
    @BindView(R.id.tv_kb)
    TextView tvKb;
    @BindView(R.id.relative_clear_cache)
    AutoRelativeLayout relativeClearCache;
    private CallWindow callWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sz_about_us);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        callWindow = new CallWindow(this);
        titleText.setText("关于系统");
        //获取当前版本的名称
        PackageManager packageManager = getPackageManager();
        try {
            PackageInfo info = packageManager.getPackageInfo(getPackageName(), 0);
            aboutUsVersion.setText("V" + info.versionName);
            tvKb.setText(DataCleanManager.getTotalCacheSize(mActivity));//缓存大小
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @OnClick({R.id.title_navigation_icon, R.id.title_text, R.id.title_menu_icon, R.id.about_us,
            R.id.about_us_version, R.id.about_tv_weixin, R.id.about_phone, R.id.about_url,
            R.id.about_service, R.id.relative_check_update, R.id.relative_clear_cache})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_navigation_icon:
                finish();
                break;
            case R.id.title_text:
                break;
            case R.id.title_menu_icon:
                finish();
                break;
            case R.id.about_us:
                break;
            case R.id.about_us_version:
                break;
            case R.id.about_tv_weixin:
                break;
            case R.id.about_phone:
                call();
                break;
            case R.id.about_url:
                break;
            case R.id.about_service:
                break;
            //检测更新
            case R.id.relative_check_update:
                checkUpdate();
                break;
            //清理缓存
            case R.id.relative_clear_cache:
                clearCache();
                break;
        }
    }

    /**
     * 拨打电话
     */
    private void call() {

        if (!isPermission(Manifest.permission.CALL_PHONE)) {
            applyPermission(new String[]{Manifest.permission.CALL_PHONE}, new PermissionListener() {
                @Override
                public void onSucceed(int requestCode, @NonNull List<String> grantPermissions) {
                    call();
                }

                @Override
                public void onFailed(int requestCode, @NonNull List<String> deniedPermissions) {
                    SnackbarHelper.show(tvKb, "您拒绝了拨打电话权限，无法正常拨打电话");
                }
            });
            return;
        }

        callWindow.setPhoneNumber(aboutPhone.getText().toString());
        callWindow.showDiaLog();


    }

    /**
     * 清理缓存
     */
    private void clearCache() {
        try {
            show("已经清除" + DataCleanManager.getTotalCacheSize(mActivity) + "缓存");
            DataCleanManager.clearAllCache(mActivity);
            tvKb.setText(DataCleanManager
                    .getTotalCacheSize(mActivity));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 检查是否更新
     */
    protected void checkUpdate() {
//        show("已经是最新版本");
        UpdateManager manager = new UpdateManager(mActivity);
        manager.isUpdate();
    }

}
